package vworld.dao.avatar;

import generated.domain.avatar.Avatar;
import generated.domain.room.Room;
import generated.domain.user.User;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import vworld.dao.HibernateUtil;

public class AvatarDAO {
	public void createAvatar(Avatar pAvatar) {
		Transaction tx = null;
		Session session = HibernateUtil.currentSession();
		try {
			tx = session.beginTransaction();
			session.save(pAvatar);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null && tx.isActive())
				tx.rollback();
		} finally {
			HibernateUtil.closeSession();
		}
	}

	public void deleteAvatar(Avatar pAvatar) {
		Transaction tx = null;
		Session session = HibernateUtil.currentSession();
		try {
			tx = session.beginTransaction();
			session.delete(pAvatar);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null && tx.isActive())
				tx.rollback();
		} finally {
			HibernateUtil.closeSession();
		}
	}

	public void saveOrUpdateAvatar(Avatar pAvatar) {
		Transaction tx = null;
		Session session = HibernateUtil.currentSession();
		try {
			tx = session.beginTransaction();
			session.saveOrUpdate(pAvatar);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null && tx.isActive())
				tx.rollback();
		}
	}

	public Avatar getAvatarByName(String avatarName) {
		try {
			Session session = HibernateUtil.currentSession();
			Query query = session
					.createQuery("from Avatar a where a.name = :name");
			query.setString("name", avatarName);

			for (Object obj : query.list()) {
				return (Avatar) obj;
			}
			return null;
		} finally {
			HibernateUtil.closeSession();
		}
	}

	public String getUserRolebyAvatarName(String avatarName) {
		try {
			Session session = HibernateUtil.currentSession();
			Query query = session.createQuery("from User u");

			for (Object obj : query.list()) {
				User u = (User) obj;
				if (u.getAvatar().getName().equalsIgnoreCase(avatarName)) {
					return u.getRole();
				}
			}
			return "";
		} finally {
			HibernateUtil.closeSession();
		}
	}

	public String getRoomNameByAvatarName(String avatarName) {
		try {
			Session session = HibernateUtil.currentSession();
			Query query = session.createQuery("from Room r");

			for (Object obj : query.list()) {
				Room r = (Room) obj;
				for (Avatar a : r.getInsideAvatars()) {
					if (a.getName().equals(avatarName)) {
						return r.getName();
					}
				}
			}
			return "";
		} finally {
			HibernateUtil.closeSession();
		}
	}

	public User getUserByAvatarName(String avatarName) {
		try {
			Session session = HibernateUtil.currentSession();
			Query query = session.createQuery("from User u");

			for (Object obj : query.list()) {
				User u = (User) obj;
				if (u.getAvatar().getName().equalsIgnoreCase(avatarName)) {
					return u;
				}
			}
			return null;
		} finally {
			HibernateUtil.closeSession();
		}
	}
}
